﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace HCSAnalyzer.Classes.General_Types
{
    [Serializable]
    public class cDataForcGlobalInfoToBeExported
    {
        public List<string> ListPhenotypeNames = new List<string>();
        public List<Color> ListPhenotypeColors = new List<Color>();
        public List<string> ListClassNames = new List<string>();
        public List<Color> ListClassColors = new List<Color>();

    }



    public class cGlobalInfoToBeExported
    {
        cGlobalInfo GlobalInfo;
        public cDataForcGlobalInfoToBeExported DataForcGlobalInfoToBeExported;

        public cGlobalInfoToBeExported(cGlobalInfo GlobalInfo)
        {
            this.GlobalInfo = GlobalInfo;
        }


        public void Save(string Path)
        {
            DataForcGlobalInfoToBeExported = new cDataForcGlobalInfoToBeExported();
            DataForcGlobalInfoToBeExported.ListPhenotypeNames = new List<string>();

            foreach (var item in GlobalInfo.ListCellularPhenotypes)
            {
                DataForcGlobalInfoToBeExported.ListPhenotypeNames.Add(item.Name);
                DataForcGlobalInfoToBeExported.ListPhenotypeColors.Add(item.ColourForDisplay);
            }

            foreach (var item in cGlobalInfo.ListWellClasses)
            {
                DataForcGlobalInfoToBeExported.ListClassNames.Add(item.Name);
                DataForcGlobalInfoToBeExported.ListClassColors.Add(item.ColourForDisplay);
            }

            if (Path == "")
            {
                SaveFileDialog CurrSavefileDialog = new SaveFileDialog();
                CurrSavefileDialog.Filter = "options file (*.OPT)|*.OPT";
                CurrSavefileDialog.FileName = "Options.OPT";
                DialogResult Res = CurrSavefileDialog.ShowDialog();
                if (Res != DialogResult.OK) return;

                if (CurrSavefileDialog.FileName == "") return;

                Path = CurrSavefileDialog.FileName;
            }

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(Path,
                                     FileMode.Create,
                                     FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, DataForcGlobalInfoToBeExported);
            stream.Close();


        }

        public void Load(string Path)
        {
            if (Path == "")
            {
                OpenFileDialog CurrOpenFileDialog = new OpenFileDialog();

                CurrOpenFileDialog.Filter = "options file (*.OPT)|*.OPT";
                CurrOpenFileDialog.Multiselect = false;
                DialogResult Res = CurrOpenFileDialog.ShowDialog();
                if (Res != DialogResult.OK) return;

                if (CurrOpenFileDialog.FileName == "") return;
                Path = CurrOpenFileDialog.FileName;
            }


            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(Path,
                                      FileMode.Open,
                                      FileAccess.Read,
                                      FileShare.Read);
            this.DataForcGlobalInfoToBeExported = (cDataForcGlobalInfoToBeExported)formatter.Deserialize(stream);
            stream.Close();
        }



    }
}
